import requests

import bs4
import re
import time
import csv
"""
思路

百度关键字 小黄车 造价

得到列表

一个一个的取源码，正则价格


"""
pattern = r'(\d{2,4})\s*元'
rr = re.compile(pattern)

# with open('page.html','rb') as f:
#     date = f.read()

# date = date.decode('utf-8')
# print(date)
# a = rr.findall(date)
# print(a)
'''
with open('result.txt','r') as f:
    date = f.read()

date = eval(date)

with open('result.csv','w',newline='') as f:
    csv_write = csv.writer(f,dialect='excel')

    for k in date.items():
        csv_write.writerow(k)




exit()
'''
max_page = 20

prices = []

def get_page_code(url='',num = None):
    if num != None:
        url = 'http://www.baidu.com/s'
        date = {
            'wd':'摩拜 造价',
            'pn':num*10,
        }
    else:
            date = {}
    try:
        response = requests.get(url = url, params = date,timeout=10)
       # print(response.url)
        if response.status_code == 200:
            response.encoding = 'utf-8'
            return response.text
        else:
            return ''
    except:
        return ''



def all_list(arr):
    result = {}
    for i in set(arr):
        result[i] = arr.count(i)
    return result

def main(prices):


    # with open('e:\ofo 造价_百度搜索.html','rb') as f:
    #     date = f.read()


    for page_num in range(max_page):
        page = get_page_code(url = '',num = page_num)
        # print(page)
        bs = bs4.BeautifulSoup(page,features="lxml")
        a = bs.select('a[data-click]:not(.m)')
        print(len(a))
        for b in a:
            print(b.text)
            page = get_page_code(b['href'])
            # page = page.decode('utf-8')
            # print(page)
            price = rr.findall(page)
            print(price)
            prices += price

            # exit()


    tsji = all_list(prices)

    # with open('result.txt','w') as f:
    #     f.write(repr(tsji))

    with open('resultmoblie.csv','w',newline='') as f:
        csv_write = csv.writer(f,dialect='excel')

        for k in tsji.items():
            csv_write.writerow(k)

    print("finish")
if __name__ == "__main__":
    main(prices = prices)